<template>
  <div class="table-default">
    <el-form :model="formData" label-width="120px" ref="form" v-loading="loading" class="pr-4" readonly>
      <el-form-item label="盘点方案名称 :" prop="plan_name" border>
        <div class="formItem">{{ formData.plan_name }}</div>
        <!-- <el-input v-model="formData.plan_name" name="plan_name"  readonly/> -->
      </el-form-item>
      <el-form-item label="分单规则 :" prop="split_order_rules">
        <div class="formItem">{{ SplitOrderRules[formData.split_order_rules] }}</div>
      </el-form-item>
      <el-form-item label="仓库编码 :" prop="wms_code">
        <div class="formItem">{{ formData.wms_code }}</div>
      </el-form-item>
      <el-form-item label="库存组织 :" prop="org_code">
        <div class="formItem">{{ OrgList[formData.org_code] }}</div>
      </el-form-item>
      <el-form-item label="盘点范围 :" prop="wms_range">
        <div class="formItem">{{ formData.wms_range }}</div>
      </el-form-item>
      <el-form-item label="备注 :" prop="remark">
        <div class="formItem">{{ formData.remark }}</div>
      </el-form-item>

      <el-form-item prop="allow_new_material">
        <el-checkbox v-model="formData.allow_new_material" label="允许盘点新增物料" disabled />
      </el-form-item>
      <!-- <el-form-item prop="no_check_order">
        <el-checkbox v-model="formData.no_check_order" label="忽略单据库存检查" disabled />
      </el-form-item> -->
      <!-- <el-form-item prop="create_adjust_order">
        <el-checkbox v-model="formData.create_adjust_order" label="创建盘盈盘亏单" disabled />
      </el-form-item> -->
      <!-- <el-form-item label="实盘数默认值 :" prop="default_qty">
        <div class="formItem">{{ formData.default_qty }}</div>
      </el-form-item> -->
      <!-- <el-form-item label="库存截止时间 :" prop="inventory_eadline">
        <div class="formItem">{{ formData.inventory_eadline == 0 ? '即时库存' : formData.inventory_eadline }}</div>
      </el-form-item> -->


      <el-dialog v-model="storageShow" title="选择仓库" width="80%" :before-close="handleClose" class="dialog">
        <createStorage @send-data="getDataHandler" :showList="showList" />
        <template #footer> </template>
      </el-dialog>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { useCreate } from '/admin/composables/curd/useCreate'
import { useShow } from '/admin/composables/curd/useShow'
import { onMounted, ref, watch, reactive } from 'vue'
import createStorage from './createStorage.vue'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'

const props = defineProps({
  primary: String | Number,
  api: String,
})

// const { formData, form, loading, submitForm, close } = useCreate(props.api, props.primary)
let resData = ref({})
let formData = ref({})

let radioValue = ref('0')
// primary为id 有即修改
console.log('primary', props.api, props.primary)

if (props.primary == null) {
  console.log('进去新增')
  formData.value = {
    default_qty: 0,
    allow_new_material: 0,
    no_check_order: 0,
    create_adjust_order: 0,
    inventory_eadline: '0',
  }
}

const emit = defineEmits(['close'])

watch(radioValue, (newVal, oldVal) => {
  if (newVal == '0') {
    console.log('库存选项改变')
    formData.value.inventory_eadline = '0'
    console.log(formData.value.inventory_eadline)
  }
})
let storageShow = ref(false)

// let sotrageClick = () => {
//   console.log('点击')
// }

let confirmStorage = () => {
  storageShow.value = false
}

let showList = ref([])

let getDataHandler = data => {
  storageShow.value = false
  formData.value.wms_range = data
}

const closeHandler = () => {
  close(() => emit('close'))
}

let OrgList = reactive({})
let SplitOrderRules = reactive({})

const enumName = () => {
  http.get(`inventory/plan/${props.primary}`).then(r => {
    r.data.data.org_code = Number(r.data.data.org_code)
    r.data.data.default_qty = Number(r.data.data.default_qty)
    resData.value = r.data.data
    console.log('详情数据', resData.value)
    if (props.primary) {
      formData.value = resData.value
      // useShow(props.api, props.primary, formData)
    }
  })
  http.get(`options/Org`).then(r => {
    // OrgList.value = r.data.data
    r.data.data.forEach(item => {
      OrgList[item.value] = item.label
    })
    console.log('组织机构', OrgList)
  })
  http.get(`options/SplitOrderRules`).then(r => {
    r.data.data.forEach(item => {
      SplitOrderRules[item.value] = item.label
    })
    console.log('盘点方案-拆单规则	', SplitOrderRules)
  })
}
enumName()

onMounted(() => {
  close(() => emit('close'))
})
</script>

<style lang="css">
.dialog {
  /* height: 85%; */
  overflow: auto;
}
.el-form-item {
  margin-bottom: 10px;
}
.btn {
  padding: 20px;
}
.formItem {
  width: 100%;
  padding-left: 10px;
  border-bottom: 1px solid #ccc;
}
</style>
